package com.grapes.view;

import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.grapes.R;
import com.grapes.business.ProcedureBusiness;
import com.grapes.model.CampusTalking;
import com.grapes.model.DailyInfo;
import com.grapes.model.NetApplication;
import com.grapes.model.RecruiteInfo;
import com.grapes.widget.PullToRefreshListView;

public class ProcedureListView extends PullToRefreshListView {
	private List<RecruiteInfo> deadlineStoredProcedures;
	private List<RecruiteInfo> noDeadlineStoredProcedures;
	private List<RecruiteInfo> applicatedProcedures;
	private List<RecruiteInfo> refusedProcedures;
    private int selectedItemIndex=-1;
	private ProcedureAdapter adapter = new ProcedureAdapter();
	private int procedureType;
	private boolean isDeadlineList = false;
	private int currentPage=0;

	public ProcedureListView(Context context) {
		super(context);
	}

	public ProcedureListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public ProcedureListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setProcedureType(int procedureType) {
		this.procedureType = procedureType;
		switch (procedureType) {
		case ProcedureBusiness.PROCEDURE_STORED:
			isDeadlineList = true;
			deadlineStoredProcedures = new LinkedList<RecruiteInfo>();
			noDeadlineStoredProcedures = new LinkedList<RecruiteInfo>();
			break;
		case ProcedureBusiness.PROCEDURE_APPLICATED:
			isDeadlineList = false;
			applicatedProcedures = new LinkedList<RecruiteInfo>();
			break;
		case ProcedureBusiness.PROCEDURE_REFUSED:
			isDeadlineList = false;
			refusedProcedures = new LinkedList<RecruiteInfo>();
			break;
		}
		setAdapter(adapter);
		loadData();
	}

	private void loadStoredProcedures() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					deadlineStoredProcedures.clear();
					noDeadlineStoredProcedures.clear();
					List<RecruiteInfo> storedProcedures = ProcedureBusiness
							.getInstance().getProcedures(ProcedureBusiness.PROCEDURE_STORED,"1234",currentPage);
					for (int index = 0; index < storedProcedures.size(); index++) {
						RecruiteInfo recruiteInfo = storedProcedures.get(index);
						if (recruiteInfo instanceof DailyInfo)
							noDeadlineStoredProcedures.add(recruiteInfo);
						else
							deadlineStoredProcedures.add(recruiteInfo);
						Message msg = new Message();
						msg.what = ProcedureManagementActivity.MSG_LOAD_STORED_OK;
						ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
						context.getHandler().sendMessage(msg);
					}
				} catch (Exception ex) {
					Message msg = new Message();
					msg.what = ProcedureManagementActivity.MSG_LOAD_STORED_FAIL;
					ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
					context.getHandler().sendMessage(msg);
				}
			}
		}).start();
	}

	private void loadApplicatedProcedures() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					applicatedProcedures.clear();
					applicatedProcedures = ProcedureBusiness.getInstance()
							.getProcedures(ProcedureBusiness.PROCEDURE_APPLICATED,"1234",currentPage);
					adapter.notifyDataSetChanged();

					Message msg = new Message();
					msg.what = ProcedureManagementActivity.MSG_LOAD_APPLICATED_OK;
					ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
					context.getHandler().sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = ProcedureManagementActivity.MSG_LOAD_APPLICATED_FAIL;
					ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
					context.getHandler().sendMessage(msg);
				}
			}
		}).start();
	}

	private void loadRefusedProcedures() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					refusedProcedures.clear();
					refusedProcedures = ProcedureBusiness.getInstance()
							.getProcedures(ProcedureBusiness.PROCEDURE_REFUSED,"1234",currentPage);
					adapter.notifyDataSetChanged();

					Message msg = new Message();
					msg.what = ProcedureManagementActivity.MSG_LOAD_REFUSED_OK;
					ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
					context.getHandler().sendMessage(msg);
				} catch (Exception e) {
					Message msg = new Message();
					msg.what = ProcedureManagementActivity.MSG_LOAD_REFUSED_FAIL;
					ProcedureManagementActivity context = (ProcedureManagementActivity) getController();
					context.getHandler().sendMessage(msg);
				}
			}
		}).start();
	}
	
	private void shareRecruiteInfo()
	{
		
	}

	private void favoriteRecruiteInfo()
	{
		
	}
	
	private void refuseRecruiteInfo()
	{
		
	}
	
	public void loadData() {
		switch (procedureType) {
		case ProcedureBusiness.PROCEDURE_STORED:
			loadStoredProcedures();
			break;
		case ProcedureBusiness.PROCEDURE_APPLICATED:
			loadApplicatedProcedures();
			break;
		case ProcedureBusiness.PROCEDURE_REFUSED:
			loadRefusedProcedures();
			break;
		}
	}

	private class CellViewHolder {
		TextView companyNameTxt;
		TextView timeThresholdTxt;
		TextView schollName;
		TextView addressTxt;
		TextView favoriteCountTxt;
		TextView commentCountTxt;
		Button shareBtn;
		Button refuseBtn;
		Button favoriteBtn;
	}

	class ProcedureAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return deadlineStoredProcedures.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CellViewHolder viewHolder;
			TextView companyNameTxt;
			TextView timeThresholdTxt;
			TextView schollName;
			TextView addressTxt;
			TextView favoriteCountTxt;
			TextView commentCountTxt;
			Button shareBtn;
			Button refuseBtn;
			Button favoriteBtn;
			final int index=position;

			if (convertView == null) {
				convertView = ((ProcedureManagementActivity) getController())
						.getLayoutInflater().inflate(R.layout.procedure_item,
								null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.timeThresholdTxt = (TextView) convertView
						.findViewById(R.id.procedure_item_date_time);
				viewHolder.companyNameTxt = (TextView) convertView
						.findViewById(R.id.procedure_item_company_name);
				viewHolder.schollName = (TextView) convertView
						.findViewById(R.id.procedure_item_school_name);
				viewHolder.addressTxt = (TextView) convertView
						.findViewById(R.id.procedure_item_address);
				viewHolder.favoriteCountTxt = (TextView) convertView
						.findViewById(R.id.procedure_item_favorite_count);
				viewHolder.commentCountTxt = (TextView) convertView
						.findViewById(R.id.procedure_item_comment_count);

				// Button
				viewHolder.shareBtn = (Button) convertView
						.findViewById(R.id.procedure_item_share_btn);
				viewHolder.refuseBtn = (Button) convertView
						.findViewById(R.id.procedure_item_refuse_btn);
				viewHolder.favoriteBtn = (Button) convertView
						.findViewById(R.id.procedure_item_favorite_btn);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			companyNameTxt = viewHolder.companyNameTxt;
			timeThresholdTxt = viewHolder.timeThresholdTxt;
			schollName = viewHolder.schollName;
			addressTxt = viewHolder.addressTxt;
			favoriteCountTxt = viewHolder.favoriteCountTxt;
			commentCountTxt = viewHolder.commentCountTxt;
			shareBtn = viewHolder.shareBtn;
			refuseBtn = viewHolder.refuseBtn;
			favoriteBtn = viewHolder.favoriteBtn;
			
			shareBtn.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					selectedItemIndex=index;
					shareRecruiteInfo();
				}
			});
			
			refuseBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					selectedItemIndex=index;
					refuseRecruiteInfo();
				}
			});
			
			favoriteBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					selectedItemIndex=index;
					favoriteRecruiteInfo();
				}
			});

			RecruiteInfo recruiteInfo = null;
			if (procedureType == ProcedureBusiness.PROCEDURE_STORED) {
				if (!isDeadlineList)// the daily info list
				{
					recruiteInfo = (RecruiteInfo) deadlineStoredProcedures
							.get(position);
					schollName.setVisibility(View.GONE);
					addressTxt.setVisibility(View.GONE);
					companyNameTxt.setText(recruiteInfo.getCompanyName());
					favoriteCountTxt.setText(recruiteInfo.getFavoriteCount());
					commentCountTxt.setText(recruiteInfo.getCommentCount());
				} else// campus talking list or net_appliction list
				{
					recruiteInfo = (RecruiteInfo) noDeadlineStoredProcedures
							.get(position);
					companyNameTxt.setText(recruiteInfo.getCompanyName());
					favoriteCountTxt.setText(recruiteInfo.getFavoriteCount());
					commentCountTxt.setText(recruiteInfo.getCommentCount());
					if (RecruiteInfo.RECRUITE_INFO_TYPE_CAMPUS_TALKING == recruiteInfo
							.getType()) {
						schollName.setVisibility(View.VISIBLE);
						addressTxt.setVisibility(View.VISIBLE);
						CampusTalking obj = (CampusTalking) recruiteInfo;
						schollName.setText(obj.getSchoolName());
						addressTxt.setText(obj.getAddress());
						timeThresholdTxt.setText(obj.getDate() + " "
								+ obj.getTime());
					} else {
						schollName.setVisibility(View.GONE);
						addressTxt.setVisibility(View.GONE);
						NetApplication obj = (NetApplication) recruiteInfo;
						timeThresholdTxt.setText(obj.getStartDate() + " "
								+ obj.getStartTime() + "--" + obj.getEndDate()
								+ " " + obj.getEndTime());
					}
				}
			} else// applicated list or refused list
			{
				if(procedureType == ProcedureBusiness.PROCEDURE_APPLICATED)
					recruiteInfo=applicatedProcedures.get(position);
				else
					recruiteInfo=refusedProcedures.get(position);
				companyNameTxt.setText(recruiteInfo.getCompanyName());
				favoriteCountTxt.setText(recruiteInfo.getFavoriteCount());
				commentCountTxt.setText(recruiteInfo.getCommentCount());
				switch (recruiteInfo.getType()) {
				case RecruiteInfo.RECRUITE_INFO_TYPE_CAMPUS_TALKING:
					schollName.setVisibility(View.VISIBLE);
					addressTxt.setVisibility(View.VISIBLE);
					CampusTalking obj = (CampusTalking) recruiteInfo;
					schollName.setText(obj.getSchoolName());
					addressTxt.setText(obj.getAddress());
					timeThresholdTxt.setText(obj.getDate() + " "
							+ obj.getTime());
					break;
				case RecruiteInfo.RECRUITE_INFO_TYPE_DAILY_INFO:
					schollName.setVisibility(View.GONE);
					addressTxt.setVisibility(View.GONE);
					break;
				case RecruiteInfo.RECRUITE_INFO_TYPE_NET_APPLICATION:
					schollName.setVisibility(View.GONE);
					addressTxt.setVisibility(View.GONE);
					NetApplication obj2 = (NetApplication) recruiteInfo;
					timeThresholdTxt.setText(obj2.getStartDate() + " "
							+ obj2.getStartTime() + "--" + obj2.getEndDate()
							+ " " + obj2.getEndTime());
					break;
				}
			}			
			return convertView;
		}
	}
}
